﻿Imports System.Runtime.InteropServices
Module SupportWords

    '调用一个窗口的窗口函数，将一条消息发给那个窗口。除非消息处理完毕，否则该函数不会返回。SendMessageBynum， SendMessageByString是该函数的“类型安全”声明形式
    <DllImport("user32", EntryPoint:="SendMessage", SetLastError:=False, _
    CharSet:=CharSet.Auto, ExactSpelling:=False, _
    CallingConvention:=CallingConvention.StdCall)> _
    Public Function SendMessage _
                  (ByVal hWnd As IntPtr, _
                   ByVal wMsg As Integer, _
                   ByVal wParam As Integer, _
                   ByRef lParam As IntPtr) As Integer
    End Function
    Public Const WM_SETREDRAW = &HB
    '取得当前活动键盘布局的名称
    <DllImport("user32", EntryPoint:="GetKeyboardLayoutName", SetLastError:=False, _
    CharSet:=CharSet.Auto, ExactSpelling:=False, _
    CallingConvention:=CallingConvention.StdCall)> _
    Public Function GetKeyboardLayoutName _
                  (ByVal pwszKLID As String) As Integer
    End Function
    '取得一个句柄，描述指定应用程序的键盘布局
<DllImport("user32", EntryPoint:="GetKeyboardLayout", SetLastError := False, _
CharSet := CharSet.Auto, ExactSpelling := False, _
CallingConvention := CallingConvention.StdCall)> _
    Public Function GetKeyboardLayout _
                  (ByVal dwLayout As Integer) As Integer
    End Function
    <DllImport("TLBINF32", EntryPoint:="loword", SetLastError:=False, _
    CharSet:=CharSet.Auto, ExactSpelling:=False, _
    CallingConvention:=CallingConvention.StdCall)> _
    Public Function loword(ByVal DWord As Integer) As Short
    End Function

    Public Structure IsInCheckCode
        Public StartCode As Integer
        Public EndCode As Integer
        Public Sub New(ByVal _StartCode As Integer, ByVal _EndCode As Integer)
            Me.StartCode = _StartCode
            Me.EndCode = _EndCode
        End Sub
    End Structure
    Public Enum EnumSplitMode
        ''' <summary>
        ''' 无分开模式
        ''' </summary>
        ''' <remarks></remarks>
        NoneSplit = 0
        ''' <summary>
        ''' 多意分隔模式
        ''' </summary>
        ''' <remarks></remarks>
        SplitMode = 1
        ThreeSplitMode = 2
    End Enum
    Public Structure SplitModeKeyWord
        Dim StartWord1 As String
        Dim StartWord2 As String
        Dim StartWord3 As String
        Dim EndWord As String
        Dim WordMode As EnumSplitMode
        Public Sub New(ByVal Word As String, ByVal _EndWord As String)
            Me.WordMode = EnumSplitMode.NoneSplit
            Me.StartWord1 = Word
            Me.EndWord = _EndWord
        End Sub
        Public Sub New(ByVal Word1 As String, ByVal Word2 As String, ByVal _EndWord As String)
            Me.WordMode = EnumSplitMode.SplitMode
            Me.StartWord1 = Word1
            Me.StartWord2 = Word2
            Me.EndWord = _EndWord
        End Sub
        Public Sub New(ByVal Word1 As String, ByVal Word2 As String, ByVal Word3 As String, ByVal _EndWord As String)
            Me.WordMode = EnumSplitMode.ThreeSplitMode
            Me.StartWord1 = Word1
            Me.StartWord2 = Word2
            Me.EndWord = _EndWord
            Me.StartWord3 = Word3
        End Sub
        Public Overrides Function ToString() As String
            If Me.WordMode = EnumSplitMode.NoneSplit Then
                Return Me.StartWord1 & "," & EndWord
            ElseIf Me.WordMode = EnumSplitMode.SplitMode Then
                Return Me.StartWord1 & "," & Me.StartWord2 & "," & Me.EndWord
            Else
                Return Me.StartWord1 & "," & Me.StartWord2 & "," & Me.StartWord3 & "," & Me.EndWord
            End If
        End Function
    End Structure
    Public Structure StFunWord
        Dim FunName As String
        Dim ParamCount As Integer
        Public Sub New(ByVal _FunName As String, ByVal _ParamCount As Integer)
            Me.FunName = _FunName
            Me.ParamCount = _ParamCount
        End Sub
        Public Sub New(ByVal _FunName As String)
            Me.FunName = _FunName
            Me.ParamCount = -1
        End Sub
    End Structure
    Public Structure VbsKeyCode
        Public SplitWord As List(Of SplitModeKeyWord)
        Public FunCode As List(Of StFunWord)
        Public SplitWordTag As Object
        Public Sub AddSplitWord(ByVal Word As String, ByVal _EndWord As String)
            Dim tmpItem As New SplitModeKeyWord
            If SplitWord Is Nothing Then SplitWord = New List(Of SplitModeKeyWord)
            tmpItem.WordMode = EnumSplitMode.NoneSplit
            tmpItem.StartWord1 = Word
            tmpItem.EndWord = _EndWord
            Me.SplitWord.Add(tmpItem)
        End Sub
        Public Sub AddSplitWord(ByVal Word1 As String, ByVal Word2 As String, ByVal _EndWord As String)
            Dim tmpItem As New SplitModeKeyWord
            If SplitWord Is Nothing Then SplitWord = New List(Of SplitModeKeyWord)
            tmpItem.WordMode = EnumSplitMode.SplitMode
            tmpItem.StartWord1 = Word1
            tmpItem.StartWord2 = Word2
            tmpItem.EndWord = _EndWord
            Me.SplitWord.Add(tmpItem)
        End Sub
        Public Sub AddSplitWord(ByVal Word1 As String, ByVal Word2 As String, ByVal Word3 As String, ByVal _EndWord As String)
            Dim tmpItem As New SplitModeKeyWord
            If SplitWord Is Nothing Then SplitWord = New List(Of SplitModeKeyWord)
            tmpItem.WordMode = EnumSplitMode.ThreeSplitMode
            tmpItem.StartWord1 = Word1
            tmpItem.StartWord2 = Word2
            tmpItem.EndWord = _EndWord
            tmpItem.StartWord3 = Word3
            Me.SplitWord.Add(tmpItem)
        End Sub
        Public Sub AddFunCode(ByVal _FunName As String, ByVal _ParamCount As Integer)
            Dim tmpItem As New StFunWord
            If FunCode Is Nothing Then FunCode = New List(Of StFunWord)
            tmpItem.FunName = _FunName
            tmpItem.ParamCount = _ParamCount
            Me.FunCode.Add(tmpItem)
        End Sub
        Public Sub AddFunCode(ByVal _FunName As String)
            If FunCode Is Nothing Then FunCode = New List(Of StFunWord)
            If _FunName.IndexOf(",") < 0 Then
                Dim tmpItem As New StFunWord(_FunName)
                Me.FunCode.Add(tmpItem)
            Else
                Dim a() As String = _FunName.Split(",")
                For i = 0 To a.Length - 1
                    If Not String.IsNullOrEmpty(a(i)) Then
                        Dim tmpItem As New StFunWord(a(i))
                        Me.FunCode.Add(tmpItem)
                    End If
                Next
            End If
        End Sub
        Public ReadOnly Property GetSplitWordString(ByVal SplitChar As Char) As String
            Get
                On Error Resume Next
                Dim Result As String = vbNullString
                For i = 0 To Me.SplitWord.Count - 1
                    If String.IsNullOrEmpty(Result) Then
                        Result = Me.SplitWord(i).ToString
                    Else
                        Result += SplitChar + Me.SplitWord(i).ToString
                    End If
                Next
                Return Result
            End Get
        End Property
        Public ReadOnly Property GetFunCodeString(ByVal SplitChar As Char) As String
            Get
                On Error Resume Next
                Dim Result As String = vbNullString
                For i = 0 To Me.FunCode.Count - 1
                    If String.IsNullOrEmpty(Result) Then
                        Result = Me.FunCode(i).ToString
                    Else
                        Result += SplitChar + Me.FunCode(i).ToString
                    End If
                Next
                Return Result
            End Get
        End Property
    End Structure
    ''' <summary>
    ''' 停止重绘
    ''' </summary>
    ''' <param name="Handle"></param>
    ''' <remarks></remarks>
    Public Sub DisablePaint(ByVal Handle As IntPtr)
        SendMessage(Handle, WM_SETREDRAW, 0, IntPtr.Zero)
    End Sub
    ''' <summary>
    ''' 允许重绘
    ''' </summary>
    ''' <param name="Handle"></param>
    ''' <remarks></remarks>
    Public Sub AllowPaint(ByVal Handle As IntPtr)
        SendMessage(Handle, WM_SETREDRAW, 1, IntPtr.Zero)
    End Sub
    Public Function IsExistsCode(ByVal Code As Integer, ByVal CodeList As List(Of IsInCheckCode)) As Boolean
        If CodeList.Count > 0 Then
            For i = 0 To CodeList.Count - 1
                If Code >= CodeList.Item(i).StartCode And Code <= CodeList.Item(i).EndCode Then
                    Return True
                End If
            Next
            Return False
        Else
            Return False
        End If
    End Function
End Module
